package denisLunchy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import javax.swing.ListCellRenderer;
import javax.xml.bind.ParseConversionEvent;

import DAO.*;

import org.apache.log4j.Logger;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Spinner;

public class View extends Composite {
	public String nameClient;
	public String surNameCLient;
	Order order;
	Dish dish;
	public String category;
	public String name;
	public int cost;
	public int count;
	public TableItem item;
	private MVC mvc = null;
	Composite addComp;
	public Spinner spnCount;
	public Text textName;
	public Text textCost;
	public Text textCount;
	private Label lblName;
	private Label lblCount;
	Table table;
	public TableItem tableItem;
	private TableItem tableItem_1;
	Button btnOrder;
	Group grpOrder;
	Composite dishMenu;
	Button btnAdd;
	Button btnRemove;
	Button btnAddDish;
	Button btnShowhide;
	private Label lbPrice;
	Combo categoryDish;
	CCombo clCmb;
	Logger log;
	int ind = 0;
	static Table tableStats;
	private TableColumn tableColumn_1;
	private TableColumn tblclmnCount;
	private TableColumn tblclmnCost;
	private TableItem tableItem_2;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public View(final MVC mvc, int style) {
		super(mvc, style);
		this.mvc = mvc;
		log = Logger.getLogger("simple");
		setLayout(null);

		grpOrder = new Group(this, SWT.NONE);
		grpOrder.setLocation(10, 10);
		grpOrder.setSize(270, 60);
		grpOrder.setText("Order");
		grpOrder.setVisible(false);

		btnOrder = new Button(grpOrder, SWT.NONE);
		btnOrder.setBounds(186, 23, 75, 25);
		btnOrder.setText("Order");

		clCmb = new CCombo(grpOrder, SWT.BORDER);
		clCmb.setBounds(10, 23, 170, 21);
		clCmb.setVisibleItemCount(5);
		clCmb.addSelectionListener(mvc.getControl());
		btnOrder.addSelectionListener(mvc.getControl());

		table = new Table(this, SWT.BORDER | SWT.CHECK | SWT.FULL_SELECTION);
		table.setBounds(10, 64, 270, 290);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(95);
		tblclmnNewColumn.setText("Category");

		final TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(72);
		tblclmnNewColumn_1.setText("Name");

		final TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(52);
		tblclmnNewColumn_2.setText("Cost");

		final TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_3.setWidth(45);
		tblclmnNewColumn_3.setText("Count");

		tableItem_1 = new TableItem(table, SWT.NONE);

		addComp = new Composite(this, SWT.BORDER);
		addComp.setBounds(286, 64, 148, 165);
		addComp.setVisible(false);
		textName = new Text(getComposite_1(), SWT.BORDER);
		textName.setBounds(60, 37, 76, 21);

		textCost = new Text(getComposite_1(), SWT.BORDER);
		textCost.setBounds(60, 65, 76, 21);

		textCount = new Text(getComposite_1(), SWT.BORDER);
		textCount.setBounds(60, 92, 76, 21);

		lblName = new Label(addComp, SWT.NONE);
		lblName.setBounds(10, 40, 55, 15);
		lblName.setText("Name");

		lblCount = new Label(addComp, SWT.NONE);
		lblCount.setBounds(10, 92, 55, 15);
		lblCount.setText("Count");

		lbPrice = new Label(addComp, SWT.NONE);
		lbPrice.setBounds(10, 64, 55, 15);
		lbPrice.setText("Price");

		btnAddDish = new Button(addComp, SWT.NONE);
		btnAddDish.setBounds(30, 130, 75, 25);
		btnAddDish.setText("Add");

		categoryDish = new Combo(addComp, SWT.NONE);
		categoryDish.setVisibleItemCount(3);
		categoryDish.setItems(new String[] { "First", "Second", "Desert" });
		categoryDish.setBounds(60, 10, 76, 23);

		Label lblType = new Label(addComp, SWT.NONE);
		lblType.setBounds(10, 16, 55, 15);
		lblType.setText("Type");
		btnAddDish.addSelectionListener(mvc.getControl());
		btnShowhide = new Button(this, SWT.NONE);
		btnShowhide.setBounds(86, 360, 117, 25);
		btnShowhide.setText("Show/Hide ");

		dishMenu = new Composite(this, SWT.BORDER);
		dishMenu.setLocation(10, 10);
		dishMenu.setSize(270, 50);

		btnAdd = new Button(dishMenu, SWT.NONE);
		btnAdd.setText("Add");
		btnAdd.setBounds(44, 10, 85, 25);

		Label lblMenu = new Label(dishMenu, SWT.NONE);
		lblMenu.setBounds(0, 0, 55, 15);
		lblMenu.setText("Menu ");

		btnRemove = new Button(dishMenu, SWT.NONE);
		btnRemove.setLocation(148, 10);
		btnRemove.setSize(85, 25);
		btnRemove.setText("Remove");
		
				spnCount = new Spinner(this, SWT.BORDER);
				spnCount.setBounds(233, 362, 47, 22);
				spnCount.setVisible(false);
				
				tableStats = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
				tableStats.setBounds(10, 10, 335, 290);
				tableStats.setLinesVisible(true);
				tableStats.setHeaderVisible(true);
				tableStats.setVisible(false);
				
				tableColumn_1 = new TableColumn(tableStats, SWT.NONE);
				tableColumn_1.setWidth(146);
				tableColumn_1.setText("Name");
				
				tblclmnCount = new TableColumn(tableStats, SWT.NONE);
				tblclmnCount.setWidth(87);
				tblclmnCount.setText("Count");
				
				tblclmnCost = new TableColumn(tableStats, SWT.NONE);
				tblclmnCost.setWidth(97);
				tblclmnCost.setText("Cost");
				
				tableItem_2 = new TableItem(tableStats, SWT.NONE);

		btnRemove.addSelectionListener(mvc.getControl());
		btnAdd.addSelectionListener(mvc.getControl());
		mvc.mntmSaveDish.addSelectionListener(mvc.getControl());
		mvc.mntmLoadDish.addSelectionListener(mvc.getControl());
		mvc.mntmAdd.addSelectionListener(mvc.getControl());
		mvc.mntmSaveList.addSelectionListener(mvc.getControl());
		mvc.mntmSaveOrd.addSelectionListener(mvc.getControl());
		mvc.mntmChange.addSelectionListener(mvc.getControl());
		btnShowhide.addSelectionListener(mvc.getControl());
		mvc.mntmStats.addSelectionListener(mvc.getControl());
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public Composite getComposite_1() {
		return addComp;
	}

	public void setComposite_1(Composite composite_1) {
		this.addComp = composite_1;
	}

	public void AddingMode() {
		if (!addComp.getVisible()) {
			addComp.setVisible(true);
		} else {
			addComp.setVisible(false);
		}
	}

	public void AddDish() {
		try {
			dish = new Dish();
			category = categoryDish.getText();
			name = textName.getText();
			cost = Integer.parseInt(textCost.getText());
			count = Integer.parseInt(textCount.getText());
			dish = contrCreateDish(category, name, cost, count);
			MVC.getModel().getMenu().addItem(dish);
			updateAll();
		} catch (NumberFormatException ex) {
			MessageBox message = new MessageBox(mvc, SWT.ERROR);
			message.setText("Error");
			message.setMessage("Invailed TextFields");
			message.open();
			log.error("Error", ex);
		}
	}

	public void RemoveDish() {
		int index = table.getSelectionIndex();
		Dish rmvDish = dishFromTable(table, index);
		for (Dish dish : mvc.getModel().getMenu().arrayDish) {
			if (dish.getName() == rmvDish.getName()) {
				dish.setStatus(true);
			}
		}
		mvc.updateTable();
	}

	public void doOrder() {
		if (isCheckedDish()) {
			order(table);
			for (Order order : MVC.getModel().getMenu().arrayOrder) {
			//	showOrderedDish(order, tableOrd);
			}
			dishMenu.setVisible(false);
			addComp.setVisible(false);

		} else {
			log.warn("Don't have selected dish");
		}
	}

	public Dish contrCreateDish(String Category, String Name, int cost,
			int count) {
		Dish dish = new Dish();
		dish.setType(Category);
		dish.setName(Name);
		dish.setCost(cost);
		dish.setCount(count);
		if (MVC.getModel().getMenu().arrayDish.isEmpty()) {
			dish.setId(0);
		} else {
			dish.setId(MVC.getModel().getMenu().arrayDish.size());
		}
		return dish;

	}

	public int countNoRemoved(ArrayList<Dish> arrayDish) {
		int counter = 0;
		for (Dish dish : arrayDish) {
			if (dish.getStatus() == false) {
				counter++;
			}
		}
		return counter;

	}

	public String[] dishForTable(Dish dish) {
		String[] lines = new String[4];
		lines[0] = dish.getType();
		lines[1] = dish.getName();
		lines[2] = Integer.toString(dish.getCost());
		lines[3] = Integer.toString(dish.getCount());
		return lines;

	}

	public void order(Table table) {
		order = new Order();
		String[] parseCl = clCmb.getItem(clCmb.getSelectionIndex()).split(",");
		nameClient = parseCl[1];
		surNameCLient = parseCl[0];
		order.setClient(order.addClient(nameClient, surNameCLient));
		if (mvc.getModel().getMenu().arrayOrder.isEmpty()) {
			order.setId(0);
		} else {
			order.setId(mvc.getModel().getMenu().arrayOrder.size());
		}
		for (int i = 0; i < table.getItemCount(); i++) {
			item = table.getItem(i);
			if (item.getChecked()) {
				order.arrayDish.add(dishFromTable(table, i));
			}
		}
		MVC.getModel().getMenu().addOrder(order);
	}

	public Dish dishFromTable(Table table, int index) {
		dish = new Dish();
		dish.setType(table.getItem(index).getText(0));
		dish.setName(table.getItem(index).getText(1));
		dish.setCost(Integer.parseInt(table.getItem(index).getText(2)));
		dish.setCount(Integer.parseInt(table.getItem(index).getText(3)));
		/*
		 * if (order.ArrayDish.isEmpty()) { dish.SetId(0); } else {
		 * dish.SetId(order.ArrayDish.size()); }
		 */

		return dish;

	}

	public void showOrderedDish(Order order, Table table) {
		table.setItemCount(order.arrayDish.size());
		for (int i = 0; i < table.getItemCount(); i++) {
			item = table.getItem(i);
			item.setChecked(false);
			item.setText(dishForTable(order.arrayDish.get(i)));
		}
	}

	public void editOrder() {

	}

	public boolean isCheckedDish() {
		for (int i = 0; i < table.getItemCount(); i++) {
			item = table.getItem(i);
			if (item.getChecked()) {
				return true;
			}
		}
		return false;
	}

	public Client clientFromView() {
		Client cl = new Client();
		String[] parseCl = clCmb.getItem(clCmb.getSelectionIndex()).split(",");
		cl.setNameClient(parseCl[1]);
		cl.setSurName(parseCl[0]);
		return cl;

	}

	public void updateAll() {
		Collections.sort(mvc.getModel().getMenu().arrayDish, new DishSort());
		table.clear(0, table.getItemCount() - 1);
		table.setItemCount(0);
		int indItem = 0;
		for (Dish dish : mvc.getModel().getMenu().arrayDish) {
			table.setItemCount(table.getItemCount() + 1);
			item = table.getItem(table.getItemCount() - 1);
			if (dish.getStatus()) {
				item.setChecked(false);
			} else {
				item.setChecked(true);
				
			}
			item.setText(dishForTable(dish));
		}

	}

	public void showStats() {

	}
}
